import { getUserCarList, addOrUpdateAmount, deleteUserCar } from "@/service/api/member.js"
import {
    GetRefundOrderTempList,
    DeleteRefundOrderTemp,
    AddRefundOrderTemp,
    UpdateRefundOrderTemp,
    ChangeRefundOrderTempNumber,
    AddOrUpdateRefundOrderTemp,
} from "@/service/api/exChange.js"

import store from "@/store/index.js"
const tempCartUtil = {
    //获取总数
    getTempCartTotal(data, callBack) {
        let total = {
            totalNum: 0,
            totalPrice: 0,
            class: 0,
        }
        if (data.length > 0) {
            data.forEach((e) => {
                e.SubItems.forEach((item) => {
                    total.class += 1
                    total.totalNum += item.Number
                    total.totalPrice += item.OrgPrice * item.Number
                })
            })
        }

        total.totalPrice = total.totalPrice.toFixed(2)
        callBack(total)
    },
    getTempCartTotalForAct(data, callBack) {
        let total = {
            totalNum: 0,
            totalPrice: 0,
            class: 0,
        }
        if (data.length > 0) {
            data.forEach((e) => {
                if (e.amount > 0) {
                    total.class += 1
                    total.totalNum += e.amount
                    total.totalPrice += e.price * e.amount
                }
            })
        }

        total.totalPrice = total.totalPrice.toFixed(2)
        callBack(total)
    },
    //获取购物车
    getRefundOrderTempList: () => {
        return new Promise((resolve, reject) => {
            let param = {
                priceType: 1,
                isTempCar: true,
            }
            GetRefundOrderTempList(param).then((res) => {
                let items = []
                if (res && res.Data && res.Data.length > 0) {
                    res.Data.forEach((e) => {
                        let filterItem = items.filter((item) => {
                            return item.ProductId == e.ProductId
                        })
                        if (filterItem.length < 1) {
                            let SubItems = []
                            SubItems.push(e)
                            let obj = {
                                ProductId: e.ProductId,
                                ProductName: e.ProductName,
                                ProductImage: e.ProductImage,
                                SubItems: SubItems,
                            }
                            items.push(obj)
                        } else {
                            filterItem[0].SubItems.push(e)
                        }
                    })
                }
                store.commit("SET_TEMP_CART", items)
                resolve(items)
            })
        })
    },

    //删除、清空购物车
    deleteRefundOrderTemp: (param) => {
        return new Promise((resolve, reject) => {
            DeleteRefundOrderTemp(param).then((res) => {
                if (res.State) {
                    tempCartUtil.getRefundOrderTempList().then((res) => {
                        resolve(true)
                    })
                }
            })
        })
    },
    checkProIsInCart(item) {
        let result = null
        let datas = store.state.tempCartData
        datas.forEach((e) => {
            if (e.ProductId == item.ProductId) {
                e.SubItems.forEach((sub) => {
                    if (sub.UnitId == item.UnitId && sub.SkuId == item.SkuId) {
                        result = sub
                    }
                })
            }
        })
        return result
    },
    getAmountInTempCart(item) {
        let amount = 0
        let datas = store.state.tempCartData
        datas.forEach((e) => {
            if (e.ProductId == item.ProductId) {
                e.SubItems.forEach((sub) => {
                    if (sub.UnitId == item.UnitId && sub.SkuId == item.SkuId) {
                        amount = sub.Number
                    }
                })
            }
        })
        return amount
    },
    getAmountInTempCartAct(item) {
        let amount = 0
        let datas = store.state.tempCartData
        datas.forEach((e) => {
            if (e.UnitId == item.UnitId && e.SkuId == item.SkuId) {
                amount = e.amount
            }
        })
        return amount
    },
    upTempCartFn(item) {
        //先判断是购物车里是已经有该商品
        return new Promise((resolve, reject) => {
            let result = tempCartUtil.checkProIsInCart(item)

            if (item.Amount <= 0 && result) {
                //删除
                let param = {
                    ids: result.ID,
                }
                tempCartUtil.deleteRefundOrderTemp(param).then((res) => {})
            } else {
                //添加到退单购物车
                let addParam = {
                    productId: item.ProductId,
                    skuId: item.SkuId,
                    number: item.Amount,
                    unitId: item.UnitId,
                    unitName: item.UnitName,
                }
                let param = []
                param.push(addParam)
                AddOrUpdateRefundOrderTemp(param).then((res) => {
                    if (res.State) {
                        tempCartUtil.getRefundOrderTempList().then((res) => {
                            resolve(true)
                        })
                    }
                })
            }
        })
    },
    upTempCartForActivityFn(item, proItem = null, isUp = true) {
        let datas = JSON.parse(JSON.stringify(store.state.tempCartData))
        let isIn = false
        datas.forEach((e) => {
            if (e.SkuId == item.SkuId && e.UnitId == item.UnitId) {
                isIn = true
                e.amount = item.Amount
            }
        })
        if (!isIn) {
            let addParam = {
                id: new Date().getTime(),
                productId: item.ProductId,
                SkuId: item.SkuId,
                amount: item.Amount,
                UnitId: item.UnitId,
                unitName: item.UnitName,
                ratio: item.Ratio,
                price: item.Price,
                isUp: isUp,
            }
            if (proItem) {
                addParam.productImage = proItem.ThumbnailUrl160
                addParam.productName = proItem.ProductName
                addParam.saleMaxPrice = proItem.SaleMaxPrice
                addParam.saleMinPrice = proItem.SaleMinPrice
            } else if (item.CartProductName) {
                addParam.productImage = item.CartProductImage
                addParam.productName = item.CartProductName
                addParam.attribute = item.CartProductAttribute
                addParam.price = item.CartProductPrice
            }
            datas.push(addParam)
        }
        let result = JSON.parse(JSON.stringify(datas))
        if (item.length < 1) {
            result = []
        }
        store.commit("SET_TEMP_CART", result)
    },
}
module.exports = {
    getTempCartTotal: tempCartUtil.getTempCartTotal,
    getTempCartTotalForAct: tempCartUtil.getTempCartTotalForAct,
    getCartTotalForSelect: tempCartUtil.getCartTotalForSelect,
    updateCart: tempCartUtil.updateCart,
    deleteRefundOrderTemp: tempCartUtil.deleteRefundOrderTemp,
    getRefundOrderTempList: tempCartUtil.getRefundOrderTempList,
    upTempCartFn: tempCartUtil.upTempCartFn,
    upTempCartForActivityFn: tempCartUtil.upTempCartForActivityFn,
    getAmountInTempCart: tempCartUtil.getAmountInTempCart,
    getAmountInTempCartAct: tempCartUtil.getAmountInTempCartAct,
}
